package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.InspectionPageReq;
import appapi.Request.record.DelMainRecReq;
import appapi.Request.record.InspectionReq;
import appapi.Vo.EquipMaintenanVo;
import appapi.Vo.MaintenanPageVo;
import appapi.Vo.record.AddMaintenanVo;
import appapi.Vo.record.EditMaintenanVo;
import appapi.common.ResponseBean;
import appapi.dto.AddEquipMaintenanDto;
import appapi.entity.Zhongjiao.MaintenanitemDto;
import appapi.service.IEquipmentMaintenanService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author djq
 * @date 2023/6/1 13:47
 * @description:
 */

@Api(tags = "设备维保记录")
@RestController
@RequestMapping(value = "/api/maintenan")
public class MaintenanController {
    @Resource
    IEquipmentMaintenanService iEquipmentMaintenanService;

    /**
     * 添加记录
     *
     * @return
     * @author lc 2023/6/3
     */
    @ApiOperation(value = "维保-添加记录", notes = "维保-添加记录")
    @PostMapping(value = "/addInfo")
    public ResponseBean addInfo(@Validated @RequestBody AddMaintenanVo vo) {
        try {
            if (iEquipmentMaintenanService.addInfo(vo) > 0) {
                return ResponseBean.success();
            }else{
                return ResponseBean.fail("保存失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail("添加维保记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 编辑记录
     *
     * @return
     * @author lc 2023/6/3
     */
    @ApiOperation(value = "维保-编辑记录", notes = "维保-编辑记录")
    @PostMapping(value = "/editInfo")
    public ResponseBean editInfo(@Validated @RequestBody EditMaintenanVo vo) {
        try {
            if (iEquipmentMaintenanService.editInfo(vo)>0) {
                return ResponseBean.updateSuccess();
            }
            return ResponseBean.updateFail();
        } catch (BusinessException exception) {
            return ResponseBean.fail("编辑记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 删除记录
     *
     * @return
     * @author lc 2023/6/3
     */
    @ApiOperation(value = "维保-删除记录", notes = "维保-删除记录")
    @PostMapping(value = "/deleteInfo")
    public ResponseBean deleteInfo(@RequestBody DelMainRecReq req) {
        try {
            if (iEquipmentMaintenanService.deleteInfo(req.getMainId())>0) {
                return ResponseBean.deleteSuccess();
            }
            return ResponseBean.deleteFail();
        } catch (BusinessException exception) {
            return ResponseBean.fail("删除记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 获取详情
     *
     * @return
     * @author lc 2023/6/3
     */

    @ApiOperation(value = "维保-获取详情", notes = "维保-获取详情")
    @GetMapping(value = "/getDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mainId", dataType = "object", value = "维保记录ID")
    })
    public ResponseBean getDetail(@Valid  Long mainId) {
        try {
            EquipMaintenanVo vo=iEquipmentMaintenanService.SelectMaintenanDetail(mainId);
            if(vo!=null)
            {
                return ResponseBean.success(vo);
            }else{
                return ResponseBean.fail("未查询到记录");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取详情异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 分页获取
     *
     * @return
     * @author lc 2023/6/3
     */

    @ApiOperation(value = "维保-分页获取", notes = "维保-分页获取")
    @GetMapping(value = "/getListPage")
    public ResponseBean<MaintenanPageVo> getListPage(@Valid InspectionPageReq req) {
        try {
            if (req.getCurrPage() == null || req.getPageSize() == null) {
                req.setCurrPage(1);
                req.setPageSize(10);
            }
            PageInfo pageInfo = iEquipmentMaintenanService.getPage(req);
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("维保分页获取异常");
        }
    }

    @ApiOperation(value = "维保-获取列表", notes = "维保-获取列表")
    @GetMapping(value = "/getList")
    public ResponseBean<MaintenanPageVo> getList(@Valid InspectionReq req) {
        try {
            List<MaintenanPageVo> data= iEquipmentMaintenanService.getList(req);
            return ResponseBean.success(data);
        } catch (BusinessException exception) {
            return ResponseBean.fail("维保获取列表异常");
        }
    }
}
